/*
 * @Author: dingyuwen ding_yuwen@163.com
 * @Date: 2023-02-08 18:03:19
 * @LastEditTime: 2023-09-01 11:16:47
 * @LastEditors: DingYuwen 43669762+DingYuwen@users.noreply.github.com
 * @Description:
 */
import type { PayloadAction } from '@reduxjs/toolkit';
import { createSlice } from '@reduxjs/toolkit';

import { SelectedTagType } from '../components/SelectedPromptTags/TagItem';
import { TagSubItem } from '../components/Sub/SubContent';
import { TagType } from '../components/TagDisplayBox/TagDisplayItemCard';

interface TagState {
  currentSub?: string;
  promptTags: SelectedTagType[];
  negativePromptTags: SelectedTagType[];
  tagSub: TagSubItem[];
  displayTags: TagType[];
  tagMap: {
    [key: string]: TagType[];
  };
}
const initialTagState: TagState = {
  currentSub: '常用提示词',
  promptTags: [],
  negativePromptTags: [],
  tagSub: [],
  displayTags: [],
  tagMap: {},
};

const initialState: TagState = initialTagState;

export const tagSlice = createSlice({
  name: 'tag',
  initialState,
  reducers: {
    setCurrentSub: (state, action: PayloadAction<string | undefined>) => {
      state.currentSub = action.payload;
      state.displayTags = action.payload
        ? state.tagMap[action.payload] || []
        : [];
    },
    setTagSub: (state, action: PayloadAction<TagSubItem[]>) => {
      state.tagSub = action.payload;
    },
    setTagMap: (state, action: PayloadAction<{ [key: string]: TagType[] }>) => {
      state.tagMap = action.payload;
    },
    setPromptTags: (state, action: PayloadAction<SelectedTagType[]>) => {
      state.promptTags = action.payload;
    },
    setNegativePromptTags: (
      state,
      action: PayloadAction<SelectedTagType[]>
    ) => {
      state.negativePromptTags = action.payload;
    },
  },
});

export const {
  setCurrentSub,
  setPromptTags,
  setNegativePromptTags,
  setTagSub,
  setTagMap,
} = tagSlice.actions;

export default tagSlice.reducer;
